package com.zhida.smartcontrol.dialog.jieridialog;

import android.text.TextUtils;

import com.jay.tools.ui.ButtonStyle;
import com.jay.tools.utils.LogUtil;
import com.zhida.smartcontrol.common.Api;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampBean;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JieRiDialogPresenter extends BasePresenterImpl<JieRiDialogContract.View> implements JieRiDialogContract.Presenter {
    private int mSize;
    private StringBuilder mSbWay = new StringBuilder();

    @Override
    public void showWayButton(String switchType) {
        mSize = getSize(switchType);
        mView.showButton(mSize);
    }

    @Override
    public void getJieRi() {
        List<JieRiBean> beanList = new ArrayList<>();
        beanList.add(new JieRiBean(0, "元旦"));
        beanList.add(new JieRiBean(1, "春节"));
        beanList.add(new JieRiBean(2, "元宵节"));
        beanList.add(new JieRiBean(3, "清明节"));
        beanList.add(new JieRiBean(4, "劳动节"));
        beanList.add(new JieRiBean(5, "端午节"));
        beanList.add(new JieRiBean(6, "中秋节"));
        beanList.add(new JieRiBean(7, "重阳节"));
        beanList.add(new JieRiBean(8, "国庆节"));
        mView.showJieRi(beanList);
    }

    @Override
    public void requestWeekByOffset(int offset) {
        String[] name;
        String[] choose;
        switch (offset) {
            case 0:
                name = new String[]{"30、31、1号", "31、1、2号", "1、2、3号"};
                choose = new String[]{"7", "14", "28"};
                break;
            case 1:
                name = new String[]{"除夕到初六", "初一到初七"};
                choose = new String[]{"127", "254"};
                break;
            case 2:
                name = new String[]{"正月十五到正月十七", "正月十四到正月十六", "正月十三到正月十五"};
                choose = new String[]{"28", "14", "7"};
                break;
            case 3:
                name = new String[]{"5、6、7号", "4、5、6号", "3、4、5号"};
                choose = new String[]{"28", "14", "7"};
                break;
            case 4:
                name = new String[]{"1号到3号", "30号到2号", "29号到1号"};
                choose = new String[]{"28", "14", "7"};
                break;
            case 5:
                name = new String[]{"初五到初七", "初四到初六", "初三到初五"};
                choose = new String[]{"28", "14", "7"};
                break;
            case 6:
                name = new String[]{"十五到十七", "十四到十六", "十三到十五"};
                choose = new String[]{"28", "14", "7"};
                break;
            case 7:
                name = new String[]{"1"};
                choose = new String[]{"1"};
                break;
            default:
                name = new String[]{"30号到6号", "1号到7号"};
                choose = new String[]{"127", "254"};
                break;
        }
        List<HolidayChoBean> beanList = new ArrayList<>();
        for (int i = 0; i < name.length; i++) {
            beanList.add(new HolidayChoBean(name[i], choose[i]));
        }
        mView.showWeek(beanList);
    }

    @Override
    public void requestSetTime(List<LampBean> codeList, String[] timeArr, List<ButtonStyle> wayList, int offset, String holidayCho) {
//        设置开关控制时间，适用于集控、二代开关
        List<String> list = new ArrayList<>();
        for (LampBean lampBean : codeList) {
            if (lampBean.isCheck()) {
                list.add(lampBean.getCode());
            }
        }
        if (list.size() <= 0) {
            mView.showMsg("请选择要操作的设备");
            return;
        }
        String mWay = getStr(wayList, mSbWay);
        if (TextUtils.isEmpty(mWay)) {
            mView.showMsg("请选择通道");
            return;
        }
        if (TextUtils.isEmpty(holidayCho)) {
            mView.showMsg("请选择日期");
            return;
        }
        int waySum = getSum(mWay);
        LogUtil.e(TAG, mWay + "==" + waySum + "==" + holidayCho + "==" + offset);
        Map<String, Object> params = new HashMap<>();
        params.put("checkedWaysString", mWay); //选中的通道组成的 字符串
        params.put("checkedWaysValue", waySum); //选中的通道的和
        params.put("holidayCho", holidayCho);
        params.put("offset", "" + offset);
        params.put("codeList", list);
        params.put("firstStartTime", timeArr[0]);
        params.put("firstEndTime", timeArr[1]);
        params.put("secondStartTime", timeArr[2]);
        params.put("secondEndTime", timeArr[3]);
        params.put("thirdStartTime", timeArr[4]);
        params.put("thirdEndTime", timeArr[5]);
        mKalleCenter.postJson(Api.SET_HOLIDAY_TIMES, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
                mView.onSuccess();
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    // true表示通道
    private String getStr(List<ButtonStyle> btnList, StringBuilder sb) {
        sb.delete(0, sb.length());
        for (int i = 0; i < mSize; i++) {
            ButtonStyle btn = btnList.get(i);
            if (btn.isChecked()) {
                sb.append(i + 1).append(",");
            }
        }
        String str = sb.toString();
        if (!TextUtils.isEmpty(str)) {
            str = str.substring(0, str.length() - 1);
        }
        return str;
    }

    private int getSum(String str) {
        String[] strArr = str.split(",");
        int sum = 0;
        for (int i = 0; i < strArr.length; i++) {
            int anInt = Integer.parseInt(strArr[i]);
            sum += Math.pow(2, anInt - 1);
        }
        return sum;
    }

    private int getSize(String switchType) {
        int type = Integer.parseInt(switchType);
        int size;
        switch (type) {
            case Const.NB_SINGLE:
            case Const.ZIG_BEE_SINGLE:
                size = 1;
                break;
            case Const.NB_DOUBLE:
                size = 2;
                break;
            case Const.SWITCH:
                size = 8;
                break;
            default:
                size = 4;
                break;
        }
        return size;
    }
}
